package com.css.zfzx.fxpg.modules.preestimate.report.repository.impl;

import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.org.dept.repository.entity.DeptEntity;
import com.css.bpm.platform.org.division.repository.entity.DivisionEntity;
import com.css.bpm.platform.utils.PlatformSessionUtils;
import com.css.zfzx.fxpg.modules.preestimate.report.repository.FxPreestimateReportNativeRepository;
import com.css.zfzx.fxpg.modules.preestimate.report.repository.entity.FxPreestimateReportEntity;
import com.css.zfzx.fxpg.modules.preestimate.report.viewobjects.FxPreestimateReportQueryParams;
import com.css.bpm.platform.utils.PlatformObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author leon
 * @date 2020-11-18
 */
@Repository
public class FxPreestimateReportNativeRepositoryImpl implements FxPreestimateReportNativeRepository {
    @PersistenceContext
    private EntityManager em;

    private static final Logger log = LoggerFactory.getLogger(FxPreestimateReportNativeRepositoryImpl.class);


    @Override
    public Page<FxPreestimateReportEntity> queryFxPreestimateReports(FxPreestimateReportQueryParams queryParams, int curPage, int pageSize) {
        StringBuilder sql = new StringBuilder("select * from fx_preestimate_report ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if (!PlatformObjectUtils.isEmpty(queryParams.getWorkspace())) {
            whereSql.append(" and workspace like :workspace");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getYearStart()) &&
                !PlatformObjectUtils.isEmpty(queryParams.getYearEnd())) {
            whereSql.append(" and year >= :yearStart");
            whereSql.append(" and year <= :yearEnd");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getYearStart()) &&
                PlatformObjectUtils.isEmpty(queryParams.getYearEnd())) {
            whereSql.append(" and year >= :yearStart");
        }
        if (PlatformObjectUtils.isEmpty(queryParams.getYearStart()) &&
                !PlatformObjectUtils.isEmpty(queryParams.getYearEnd())) {
            whereSql.append(" and year <= :yearEnd");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getUnit())) {
            whereSql.append(" and unit like :unit");
        }

        String userId = PlatformSessionUtils.getUserId();
        // 获取当前用户的行政区域
        DeptEntity mainDept = PlatformAPI.getOrgAPI().getUserAPI().getMainDept(userId);
        String divisionId = mainDept.getDivisionId();
        String province = "";
        if (StringUtils.isNotEmpty(divisionId)) {
            DivisionEntity divisionEntity = PlatformAPI.getDivisionAPI().getDivisionById(divisionId);
            province = divisionEntity.getDivisionName();
            if (StringUtils.isNotEmpty(province)) {
                whereSql.append(" and unit like :province");
            }
        }


        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), FxPreestimateReportEntity.class);
        StringBuilder countSql = new StringBuilder("select count(*) from fx_preestimate_report ");
        Query countQuery = this.em.createNativeQuery(countSql.append(whereSql).toString());
        if (StringUtils.isNotEmpty(divisionId) && StringUtils.isNotEmpty(province)) {
            query.setParameter("province", "%" + province + "%");
            countQuery.setParameter("province", "%" + province + "%");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getWorkspace())) {
            query.setParameter("workspace", "%" + queryParams.getWorkspace() + "%");
            countQuery.setParameter("workspace", "%" + queryParams.getWorkspace() + "%");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getYearStart())) {
            query.setParameter("yearStart", queryParams.getYearStart());
            countQuery.setParameter("yearStart", queryParams.getYearStart());
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getYearEnd())) {
            query.setParameter("yearEnd", queryParams.getYearEnd());
            countQuery.setParameter("yearEnd", queryParams.getYearEnd());
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getUnit())) {
            query.setParameter("unit", "%" + queryParams.getUnit() + "%");
            countQuery.setParameter("unit", "%" + queryParams.getUnit() + "%");
        }
        Pageable pageable = PageRequest.of(curPage - 1, pageSize);
        query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
        query.setMaxResults(pageable.getPageSize());
        List<FxPreestimateReportEntity> list = query.getResultList();
        BigInteger count = (BigInteger) countQuery.getSingleResult();
        Page<FxPreestimateReportEntity> fxPreestimateReportPage = new PageImpl<>(list, pageable, count.longValue());
        return fxPreestimateReportPage;
    }
}
